package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Region;
import cn.wolfcode.luowowo.article.query.DestinationQuery;
import cn.wolfcode.luowowo.article.query.RegionQuery;
import cn.wolfcode.luowowo.article.query.StrategyQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IRegionService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyService;
import cn.wolfcode.luowowo.common.util.AJaxResult;
import cn.wolfcode.luowowo.hotel.domain.Hotel;
import cn.wolfcode.luowowo.hotel.query.HotelQuery;
import cn.wolfcode.luowowo.hotel.service.IHotelService;
import cn.wolfcode.luowowo.hotel.service.IHotelThemeService;
import cn.wolfcode.luowowo.search.query.HotelSearchQuery;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.IHotelTemplateSearchService;
import cn.wolfcode.luowowo.search.service.ISearchService;
import cn.wolfcode.luowowo.search.template.HotelTemplate;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @author hslef
 */
@Controller
@RequestMapping("hotel")
public class HotelController {
    @Reference
    private IHotelService hotelService;

    @Reference
    private IHotelThemeService hotelThemeService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IRegionService regionService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private ISearchService searchService;

    @Reference
    private IHotelTemplateSearchService hotelTemplateSearchService;


    @RequestMapping("")
    public String hotel(Model model) {
        List<Destination> hotelCity = hotelService.getDestByDisCountHotel();
        model.addAttribute("hotelCity", hotelCity);
        model.addAttribute("themes", hotelThemeService.query());
        List<Destination> chinaDests = regionService.getHotDestByRegionId(-1L);
        model.addAttribute("dest", chinaDests.subList(0, 15));
        RegionQuery qo = new RegionQuery();
        qo.setIshot(1);
        List<Region> hotRegions = regionService.selectForList(qo);
        List<Map<String, Object>> overseas = new ArrayList<>();
        for (Region hotRegion : hotRegions) {
            Map<String, Object> map = new HashMap<>();
            List<Destination> hotDests = regionService.getHotDestByRegionId(hotRegion.getId());
            map.put("region", hotRegion);
            map.put("hotDests", hotDests);
            overseas.add(map);
        }
        model.addAttribute("overseas", overseas);
        return "hotel/hotel";
    }

    @RequestMapping("theme")
    public String theme(Long id, Model model) {
        List<Hotel> hotels = hotelService.getListByThemeId(id);
        List<Destination> dests = hotelService.getDestsByHotels(hotels);
        model.addAttribute("dests", dests);
        return "hotel/hotelTpl";
    }

    @RequestMapping("themeCity")
    public String themeCity(HotelQuery qo, Model model) {
        qo.setDisCount(true);
        PageInfo<Hotel> hotels = hotelService.query(qo);
        model.addAttribute("hotelScore", hotels.getList());
        return "hotel/hotelCityTpl";
    }

    @RequestMapping("order")
    public String order(Model model, @ModelAttribute("qo") HotelSearchQuery qo) {
        Destination destination = null;
        if (StringUtils.hasLength(qo.getKeyword())){
            destination = destinationService.getByName(qo.getKeyword());
            if(destination == null) {
                return highLightSearch(model, qo);
            }
        }else {
            destination = destinationService.getByName(qo.getDestName());
        }
        dataShare(model, destination.getId(),qo);
        return "hotel/dingjiudian";
    }

    @RequestMapping("orderAjax")
    public String orderAjax(Model model, @ModelAttribute("qo") HotelSearchQuery qo) {
        Page<HotelTemplate> hotelTemplates = hotelTemplateSearchService.searchHotel(qo);
        model.addAttribute("page",hotelTemplates);
        return "hotel/hotelPageTpl";
    }


    @RequestMapping("getGroupCount")
    @ResponseBody
    public Map<String,List<StatisVO>> getGroupCount(HotelSearchQuery qo){
        Map<String,List<StatisVO>> map = new HashMap<>();
        List<StatisVO> brandList = hotelTemplateSearchService.conditionGroupByQoChange("groupBrand","brand",qo);
        map.put("brandList",brandList);
        List<StatisVO> typeList = hotelTemplateSearchService.conditionGroupByQoChange("groupType","typeName",qo);
        map.put("typeList",typeList);
        List<StatisVO> levelList = hotelTemplateSearchService.conditionGroupByQoChange("groupLevel","level",qo);
        map.put("levelList",levelList);
        List<StatisVO> faclList = hotelTemplateSearchService.conditionGroupByQoChange("groupFac","facilities",qo);
        map.put("faclList",faclList);
        return map;
    }

    @RequestMapping("high")
    public String highLightSearch(Model model, HotelSearchQuery qo) {
        qo.setPageSize(5);
        qo.setOrderBy("commentNum");
        Page<HotelTemplate> page = searchService.searchWithHighlight(HotelTemplate.INDEX_NAME,
                HotelTemplate.TYPE_NAME, HotelTemplate.class, qo,
                "destName", "info", "location", "english", "name");
        model.addAttribute("page", page);
        model.addAttribute("qo", qo);
        return "hotel/hotelSearch";
    }

    @RequestMapping("highAjax")
    public String highAjax(Model model, HotelSearchQuery qo) {
        qo.setOrderBy("commentNum");
        Page<HotelTemplate> page = searchService.searchWithHighlight(HotelTemplate.INDEX_NAME,
                HotelTemplate.TYPE_NAME, HotelTemplate.class, qo,
                "destName", "info", "location", "english", "name");
        model.addAttribute("page", page);
        model.addAttribute("qo", qo);
        return "hotel/hotelPageTpl";
    }

    private void dataShare(Model model, Long destId,HotelSearchQuery qo) {
        Destination destination = destinationService.getById(destId);
        qo.setDestName(destination.getName());
        if (destination.getParent() != null) {
            Destination surround = destinationService.getById(destination.getParent().getId());
            model.addAttribute("surround", surround);
        }
        List<Map<String, Object>> avgPrice = hotelTemplateSearchService.getAvgPrice(qo);
        model.addAttribute("avgPrice",avgPrice);
        List<Destination> toasts = destinationService.getToasts(destId);
        Collections.reverse(toasts);
        model.addAttribute("toasts", toasts);
        model.addAttribute("source", destination);
        /*model.addAttribute("themes", hotelThemeService.query());*/
        Page<HotelTemplate> hotelTemplates = hotelTemplateSearchService.searchHotel(qo);
        model.addAttribute("page", hotelTemplates);
        List<StatisVO> brandList = hotelTemplateSearchService.conditionGroupByQo("groupBrand","brand",qo);
        model.addAttribute("brandList",brandList);
        List<StatisVO> typeList = hotelTemplateSearchService.conditionGroupByQo("groupType","typeName",qo);
        model.addAttribute("typeList",typeList);
        List<StatisVO> levelList = hotelTemplateSearchService.conditionGroupByQo("groupLevel","level",qo);
        model.addAttribute("levelList",levelList);
        List<StatisVO> faclList = hotelTemplateSearchService.conditionGroupByQo("groupFac","facilities",qo);
        model.addAttribute("faclList",faclList);
        List<StatisVO> themes = hotelTemplateSearchService.conditionGroupByQo("groupTheme","themeName",qo);
        model.addAttribute("themes",themes);

    }

    @RequestMapping("/{destId}")
    public String orderBydestId(@PathVariable Long destId, @ModelAttribute("qo") HotelSearchQuery qo, Model model) {
        dataShare(model, destId,qo);
        return "hotel/dingjiudian";
    }

    @RequestMapping("checkKeyword")
    @ResponseBody
    public AJaxResult checkKeyword(String keyword){
        Destination dest = destinationService.getByName(keyword);
        if(dest == null){
            AJaxResult aJaxResult = new AJaxResult();
            aJaxResult.mark("");
            aJaxResult.setCode(66);
            return aJaxResult;
        }else {
            return AJaxResult.SUCCESS();
        }
    }
}
